const multer = require('multer');
const path = require('path');
const sd = require('silly-datetime');
const mkdirp = require('mkdirp');
let tools = {
    multer(){
        //配置
        var storage = multer.diskStorage({
            //配置上传的目录
            destination: async (req, file, cb) => {
                //1.获取当前日期 20210412
                let day = sd.format(new Date(),"YYYYMMDD");
                //2.public/images/20210412
                let dir = path.join("public/images",day);
                //3.按照日期生成图片存储目录 mkdirp是一个异步方法
                await mkdirp(dir);
                cb(null, dir)
            },
            //修改上传后的文件名称
            filename: function (req, file, cb) {
                //1.获取后缀名
                let extname = path.extname(file.originalname);
                //2.根据时间戳生成文件名
                cb(null, Date.now() + extname);
            }
        })
        var upload = multer({ storage: storage })
        return upload;
    },
    
}

module.exports = tools;
